<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <div th:replace="/common/head"></div>
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>数据恢复</legend>
</fieldset>
<form class="layui-form">
    <div class="layui-row">
        <div class="layui-col-md4 layui-col-md-offset4">
            <div class="layui-form-item">
                <label class="layui-form-label">IP地址</label>
                <div class="layui-input-inline">
                    <input type="text" name="ip" value="127.0.0.1" lay-verify="required" placeholder="请输入IP地址"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux" style="color: red !important;">必填项</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">端口</label>
                <div class="layui-input-inline">
                    <input type="number" name="port" value="3306" lay-verify="required" placeholder="请输入端口"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux" style="color: red!important;">必填项</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-inline">
                    <input type="text" name="userName" value="root" lay-verify="required" placeholder="请输入用户名"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux" style="color: red !important;">必填项</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                    <input type="password" name="password" value="123456" lay-verify="required" placeholder="请输入密码"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux" style="color: red !important;">必填项</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">新建数据库</label>
                <div class="layui-input-inline">
                    <input type="radio" name="isCreateDb" value="1" title="是">
                    <input type="radio" name="isCreateDb" value="0" title="否" checked>
                </div>
                <div class="layui-form-mid layui-word-aux" style="color: red !important;">必填项</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">数据库名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="databaseName" value="test" lay-verify="required" placeholder="请输入数据库名称"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux" style="color: red !important;">必填项</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">恢复文件</label>
                <div class="layui-input-inline">
                    <select name="filePath" lay-verify="required" lay-search>
                        <option value="" selected>请选择</option>
                        <option th:each="entity:${mysqlDumpList}" th:value="${entity.filePath}"
                                th:text="${entity.fileName} + ' (数据库：' + ${entity.databaseName}+')'"></option>
                    </select>
                </div>
                <div class="layui-form-mid layui-word-aux" style="color: red !important;">必选项</div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="submit" class="layui-btn" lay-submit lay-filter="submitForm">开始恢复</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </div>
    </div>
</form>

<div th:replace="/common/js"></div>
<script>
    layui.use(['element', 'form'], function () {
        var element = layui.element
            , form = layui.form
            , $ = layui.jquery;

        var active = {};

        // 监听提交
        form.on('submit(submitForm)', function (obj) {
            layer.confirm('数据库恢复为敏感操作，确定恢复吗？', {icon: 0, title: '系统提示'}, function () {
                layer.load(1, {shade: 0.1});
                $.ajax({
                    url: '/backup/mysqlRecover/recover',
                    type: 'post',
                    data: obj.field,
                    dataType: 'json',
                    success: function (res) {
                        if (res.code == 200) {
                            layer.msg('恢复成功', {icon: 1, time: 2000}, function () {
                                window.location.reload();
                            });
                        } else {
                            layer.msg('恢复失败', {icon: 2, time: 2000}, function () {
                                layer.closeAll();
                            });
                        }
                    },
                    error: function () {
                        layer.msg('出错了请稍后再试或联系管理员', {icon: 2, time: 2000}, function () {
                            layer.closeAll();
                        });
                    }
                });
            });
            return false;
        });
    });
</script>
</body>
</html>
